//
//  RBGColorManager.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 24/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGColorManager.h"

@implementation RBGColorManager

+(void) changeTextColorArray:(NSArray *)textes
                   withColor:(UIColor *)textColor
{
    for(UITextView *textview in textes)
    {
        textview.textColor = textColor;
    }
}

+(void) changeTextColorSubViews:(UIView *)view
                      withColor:(UIColor *)textColor
{
    for(UIView *subview in [view subviews])
    {
        if([subview isKindOfClass:[UILabel class]]){
            UILabel * textview = (UILabel *)subview;
            textview.textColor = textColor;
            
        }else{
            
            if([subview isKindOfClass:[UITextField class]]){
                UITextField * textField = (UITextField *)subview;
                textField.textColor = textColor;
                [textField setValue:textColor forKeyPath:@"_placeholderLabel.textColor"];
            }else{
                if([subview isKindOfClass:[UITextView class]]){
                    UITextView * textView = (UITextView *)subview;
                    textView.textColor = textColor;
                    //[textView setValue:textColor forKeyPath:@"_placeholderLabel.textColor"];
                }else{
                    if([subview isKindOfClass:[UIView class]]){
                        [self changeTextColorSubViews:subview withColor:textColor];
                    }
                }
            }
        }
        
    }
    
}

+(UIColor *)colorFromRGBHexString:(NSString *)colorString {
    if(colorString.length == 7) {
        const char *colorUTF8String = [colorString UTF8String];
        int r, g, b; sscanf(colorUTF8String, "#%2x%2x%2x", &r, &g, &b);
        return [UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:1.0];
    }
    return nil;
}

+ (UIColor *)getUIColorObjectFromHexString:(NSString *)hexStr alpha:(CGFloat)alpha
{
    // Convert hex string to an integer
    unsigned int hexint = [self intFromHexString:hexStr];
    
    // Create color object, specifying alpha as well
    UIColor *color =
    [UIColor colorWithRed:((CGFloat) ((hexint & 0xFF0000) >> 16))/255
                    green:((CGFloat) ((hexint & 0xFF00) >> 8))/255
                     blue:((CGFloat) (hexint & 0xFF))/255
                    alpha:alpha];
    
    return color;
}




+ (unsigned int)intFromHexString:(NSString *)hexStr
{
    unsigned int hexInt = 0;
    
    // Create scanner
    NSScanner *scanner = [NSScanner scannerWithString:hexStr];
    
    // Tell scanner to skip the # character
    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
    
    // Scan hex value
    [scanner scanHexInt:&hexInt];
    
    return hexInt;
}

+ (UIImage *)colorImageWithColor:(UIColor *)color
                        andImage:(UIImage *)image
{
    // Make a rectangle the size of your image
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    // Create a new bitmap context based on the current image's size and scale, that has opacity
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
    // Get a reference to the current context (which you just created)
    CGContextRef c = UIGraphicsGetCurrentContext();
    // Draw your image into the context we created
    [image drawInRect:rect];
    // Set the fill color of the context
    CGContextSetFillColorWithColor(c, [color CGColor]);
    // This sets the blend mode, which is not super helpful. Basically it uses the your fill color with the alpha of the image and vice versa. I'll include a link with more info.
    CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
    // Now you apply the color and blend mode onto your context.
    CGContextFillRect(c, rect);
    // You grab the result of all this drawing from the context.
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    // And you return it.
    return result;
}

+(void) changeImageColorSubViews:(UIView *)view
                       withColor:(UIColor *)imageColor{
    
    for(UIView *subview in [view subviews])
    {
        if([subview isKindOfClass:[UIImageView class]]){
            UIImageView * imageView = (UIImageView *)subview;
            [imageView setImage:[RBGColorManager colorImageWithColor:imageColor andImage:imageView.image]];

        }else{
            if([subview isKindOfClass:[UIView class]]){
                [self changeImageColorSubViews:subview withColor:imageColor];
            }
        }
    }
}

+ (UIColor *) colorFromHexString:(NSString *)hexString {
    NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    if([cleanString length] == 3) {
        cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
                       [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                       [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                       [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
    }
    if([cleanString length] == 6) {
        cleanString = [cleanString stringByAppendingString:@"ff"];
    }
    
    unsigned int baseValue;
    [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
    
    float red = ((baseValue >> 24) & 0xFF)/255.0f;
    float green = ((baseValue >> 16) & 0xFF)/255.0f;
    float blue = ((baseValue >> 8) & 0xFF)/255.0f;
    float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
@end
